<?php
/*********************/
/*                   */
/*  购物车模块       */
/*   BY voiet        */
/*   2009-11-26更新    */
/*                   */
/*********************/
class cmd_cart extends mdl_cart
{
	
	function getcartweight( )
	{
			$aCart = $this->getcart( "g" );
			$acart['kr']['weight'] = 0;
			$acart['cn']['weight'] = 0;
			$acart['cart']['weight'] = 0;
			foreach ( $aCart['cart'] as $k => $v )
			{
					
					$pids = explode('-',$k);
					$objGoods = $this->system->loadmodel( "trading/goods" );
					$aField=array("brand_id","type_id","cat_id","weight");
					$abrand = $objGoods->getfieldbyid( $pids[0],$aField );
					
					if($abrand['brand_id']>=6 && $abrand['brand_id']<=16)
					{
						$acart['kr']['weight'] += $abrand['weight'] * $v;
						$acart['kr'][$k] = $v;
					}
					else
					{
						$acart['cn']['weight'] += $abrand['weight'] * $v;
						$acart['cn'][$k] = $v;
					}
					
					$acart['cart']['weight'] += $abrand['weight'] * $v;
					
			}
			return $acart;
	}
	
	function checkoutinfo( &$aCart, &$aMember, $aParam = null )
	{
			$sale = $this->system->loadmodel( "trading/sale" );
			$trading = $sale->getcartobject( $aCart, $aMember['member_lv_id'], true );
			$trading['total_amount'] = $trading['totalPrice'];
			$myacart =$this->getcartweight();
			$trading['weight_cn'] = $myacart['cn']['weight'] ? $myacart['cn']['weight'] : 0;
			$trading['weight_kr'] = $myacart['kr']['weight'] ? $myacart['kr']['weight'] : 0;
			if ( $aParam['shipping_id'] )
			{
					$shipping = $this->system->loadmodel( "trading/delivery" );
					$aShip = $shipping->getdltypebyarea( $aParam['area'], 0, $aParam['shipping_id'] );
					if ( $trading['exemptFreight'] == 1 )
					{
							$trading['cost_freight'] = 0;
					}
					else
					{
						//2009-11-26 voiet 更新运费
						if($trading['weight_kr']>0 && $trading['weight_cn']>0){
							if($aParam['shipping_id']==15)$aShip_cn = $shipping->getdltypebyarea( $aParam['area'], 0, 3 );
							elseif($aParam['shipping_id']==16)$aShip_cn = $shipping->getdltypebyarea( $aParam['area'], 0, 2 );
							elseif($aParam['shipping_id']==17)$aShip_cn = $shipping->getdltypebyarea( $aParam['area'], 0, 1 );
							elseif($aParam['shipping_id']==18)$aShip_cn = $shipping->getdltypebyarea( $aParam['area'], 0, 13 );
							$trading['cost_freight_cn'] = cal_fee( $aShip_cn[0]['expressions'], $myacart['cn']['weight'], $trading['pmt_b']['totalPrice'], $aShip_cn[0]['price'] );
							$trading['cost_freight_kr'] = cal_fee( $aShip[0]['expressions'], $myacart['kr']['weight'], $trading['pmt_b']['totalPrice'], $aShip[0]['price'] );
							$trading['cost_freight'] = $trading['cost_freight_cn'] + $trading['cost_freight_kr'];
						}
						else $trading['cost_freight'] = cal_fee( $aShip[0]['expressions'], $trading['weight'], $trading['pmt_b']['totalPrice'], $aShip[0]['price'] );
						if($aParam['shipping_id'] == 13){
							if($aMember['member_id'] == 2161 || $aMember['member_id'] == 2204 || $aMember['member_id'] == 6166 )
								$trading['cost_freight'] = 0;
							elseif($aMember['member_id']==6318)$trading['cost_freight'] = 10;
						}	
						
						
					}
					$trading['shipping_id'] = $aParam['shipping_id'];
					if ( $aParam['is_protect'] == "true" && $aShip[0]['protect'] )
					{
							$trading['is_protect'] = 1;
							$trading['cost_protect'] = max( $aShip[0]['protect_rate'] * $trading['totalPrice'], $aShip[0]['minprice'] );
					}
					$trading['total_amount'] += $trading['cost_freight'] + $trading['cost_protect'];
			}
			if ( $this->system->getconf( "site.trigger_tax" ) )
			{
					$trading['is_tax'] = 1;
					if ( !isset( $aParam['is_tax'] ) || $aParam['is_tax'] == "true" )
					{
								$trading['tax_checked'] = "checked";
								$trading['cost_tax'] = $trading['totalPrice'] * $this->system->getconf( "site.tax_ratio" );
								$trading['total_amount'] += $trading['cost_tax'];
					}
					$trading['tax_rate'] = $this->system->getconf( "site.tax_ratio" );
			}
			if ( $aParam['payment'] )
			{
					$payment = $this->system->loadmodel( "trading/payment" );
					$aPay = $payment->getpaymentbyid( $aParam['payment'] );
					$trading['cost_payment'] = $aPay['fee'] * $trading['total_amount'];
					$trading['total_amount'] += $trading['cost_payment'];
			}
			if($aMember['member_lv_id']>=4)$trading['score_g']=0;
			else $trading['score_g'] = $trading['pmt_b']['totalGainScore'];
			
			$trading['pmt_amount'] = $trading['pmt_b']['totalPrice'] - $trading['totalPrice'];
			$trading['member_id'] = $aMember['member_id'];
			$order = $this->system->loadmodel( "trading/order" );
			$newNum = $order->getorderdecimal( $trading['total_amount'] );
			$trading['discount'] = $trading['total_amount'] - $newNum;
			$trading['total_amount'] = $newNum;
			$oCur = $this->system->loadmodel( "system/cur" );
			$currency = $oCur->getcur( $aParam['cur'] );
			if ( $currency['cur_code'] )
			{
					$trading['cur_rate'] = $currency['cur_rate'];
			}
			else
			{
					$trading['cur_rate'] = 1;
			}
			$trading['final_amount'] = $newNum * $trading['cur_rate'];
			$trading['cur_sign'] = $currency['cur_sign'];
			$trading['cur_display'] = $this->system->request['cur'];
			$trading['cur_code'] = $currency['cur_code'];
			return $trading;
	}

}

?>